package com.ktjiaoyu.mqrabbitmqconsumer.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Date;

@Component
public class ReceiveHandler {
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    private MailProperties mailProperties;
    @Autowired
    private TemplateEngine templateEngine;


    @RabbitListener(
            bindings=@QueueBinding(
                value = @Queue(value = "queue_email",declare = "true"),
                exchange = @Exchange(value = "topic.exchange",
                        type = ExchangeTypes.TOPIC,
                        ignoreDeclarationExceptions = "true"),
                key = {"topic.#.email.#","email.*"}
            )
    )
    public void receive_email(Message message, Channel channel) {
        System.out.println("邮件服务："+message);
        MessageHeaders headers = message.getHeaders();
        long tags=(Long)headers.get(AmqpHeaders.DELIVERY_TAG);

        try {
            // 再去发送邮件
            MimeMessage msg=javaMailSender.createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(msg);
            //发件人
            helper.setFrom(mailProperties.getUsername());
            //收件人
            helper.setTo("3246180091@qq.com");
            //主题
            helper.setSubject("入职欢迎邮件");
            //发送日期
            helper.setSentDate(new Date());
            //邮件内容
            Context context=new Context();
            context.setVariable("name","永祥");
            context.setVariable("posName","架构师");
            context.setVariable("joblevelName","T7");
            context.setVariable("departmentName","开发部");
            String mail = templateEngine.process("mail", context);
            helper.setText(mail,true);
            //发送邮件
            javaMailSender.send(msg);
            System.out.println("邮件发送成功！");
            int s=1/0;
            //告诉成功了
            channel.basicAck(tags,false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                //还给她消息
                channel.basicNack(tags,false,true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }


    @RabbitListener(
            bindings=@QueueBinding(
                    value = @Queue(value = "queue_sms",declare = "true"),
                    exchange = @Exchange(value = "topic.exchange",
                            type = ExchangeTypes.TOPIC,
                            ignoreDeclarationExceptions = "true"),
                    key = {"topic.#.sms.#","sms.*"}
            )
    )
    public void receive_sms(String msg){
        System.out.println("短信服务："+msg);
    }
}
